#include "mpu6050.h"

MPU6050::MPU6050()
{
    if(init())
        printf("MPU6050 init success !");
    else
        printf("MPU6050 init faild !");
}

bool MPU6050::init()    // 初始化 mpu6050
{
    wiringPiSetup () ;
    iic_fd = wiringPiI2CSetup (SlaveAddress);
    if (iic_fd >= 0) // fd 为负数，说明IIC连接失败
    {
        printf("fd = %d\n",iic_fd);
        wiringPiI2CWriteReg8(iic_fd,PWR_MGMT_1,0x00); // 开启温度检测 关闭休眠
        wiringPiI2CWriteReg8(iic_fd,SMPLRT_DIV, 0x07);
        wiringPiI2CWriteReg8(iic_fd,CONFIG, 0x06);
        wiringPiI2CWriteReg8(iic_fd,GYRO_CONFIG, 0x18);
        wiringPiI2CWriteReg8(iic_fd,ACCEL_CONFIG, 0x01);
        return 1;
    }
    return 0;
}

int MPU6050::getData(int reg_address)    // 读取制定寄存器中的数据
{
    return 0;
//   return (wiringPiI2CReadReg8(iic_fd,reg_address) << 8)
//         + wiringPiI2CReadReg8(iic_fd,reg_address + 1);
}

int MPU6050::accX(){ return getData(ACCEL_XOUT_H);   }
int MPU6050::accY(){ return getData(ACCEL_YOUT_H);   }
int MPU6050::accZ(){ return getData(ACCEL_ZOUT_H);   }
int MPU6050::gyroX(){ return getData(GYRO_XOUT_H);   }
int MPU6050::gyroY(){ return getData(GYRO_YOUT_H);   }
int MPU6050::gyroZ(){ return getData(GYRO_ZOUT_H);   }

